package com.movitech.mobile.cz.base.common.upload;

import com.movitech.mobile.cz.base.common.license.LicenseUtil;
import com.movitech.mobile.cz.base.common.util.NumberUtils;
import com.movitech.mobile.cz.base.common.util.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("uploadFile")
public class UploadFile implements UploadAdittionActions{

	@Override
	public List<UploadResultVO> doFileUpload(Map<String, MultipartFile> mapMultipartFile) throws Exception{
		System.out.println("===========UploadFile-->doFileUpload");
		List<UploadResultVO> lstUploadResultVO = new ArrayList<UploadResultVO>();

		LicenseUtil.INSTANCE.validate();
		if (mapMultipartFile!=null && mapMultipartFile.size()>0) {
			for(String key:mapMultipartFile.keySet()){
				MultipartFile multipartFile = mapMultipartFile.get(key);
	            if(multipartFile!=null) {
	            	String realFileName = multipartFile.getOriginalFilename();//真实上传文件名
					realFileName = URLDecoder.decode(realFileName, "UTF-8");
					if(StringUtils.isNotEmpty(realFileName)){
						String[] arrRealFileNameStr = realFileName.replaceAll("\\\\", "/").split("/");
						realFileName = arrRealFileNameStr[arrRealFileNameStr.length-1];
					}
					///
					if(StringUtils.isNotEmpty(realFileName)){
		                System.out.println("上传的文件名为-----:"+realFileName);  // 扩展名格式：
						String fileType = "";
						if (realFileName.lastIndexOf(".") >= 0) {
							fileType = realFileName.substring(realFileName.lastIndexOf("."));
						}

						String filePath = uploadFileAction(multipartFile);
						if(StringUtils.isNotEmpty(filePath)) {
							UploadResultVO uploadResultVo = new UploadResultVO();
							uploadResultVo.setAttachName(realFileName);
							uploadResultVo.setAttachSize(multipartFile.getSize() + "");
							uploadResultVo.setAttachmentSubType("1");/// 1：附件 2：图片 3：视频 4：录音
							uploadResultVo.setAttachPath(filePath);
							lstUploadResultVO.add(uploadResultVo);
						}
		            }
	            }
	        }
		}
		return lstUploadResultVO;
	}

	@Override
	public String uploadFileAction(MultipartFile multipartFile) {
		System.out.println("===========UploadFile-->uploadFileAction");
		return null;
	}

	@Override
	public List<UploadResultVO> doFileUpload(List<MultipartFile> lstFile) throws Exception {
		System.out.println("===========UploadFile-->doFileUpload");
		List<UploadResultVO> lstUploadResultVO = new ArrayList<UploadResultVO>();

		if (CollectionUtils.isNotEmpty(lstFile)) {
			for(MultipartFile multipartFile :lstFile){
	            if(multipartFile!=null) {
	            	String realFileName = multipartFile.getOriginalFilename();//真实上传文件名
					if(StringUtils.isNotEmpty(realFileName)){
						String[] arrRealFileNameStr = realFileName.replaceAll("\\\\", "/").split("/");
						realFileName = arrRealFileNameStr[arrRealFileNameStr.length-1];
					}
					///
	                System.out.println("上传的文件名为ENCODE-----:"+new String(realFileName.getBytes(), "UTF-8"));  // 扩展名格式：
		            if(StringUtils.isNotEmpty(realFileName)){
		                System.out.println("上传的文件名为-----:"+realFileName);  // 扩展名格式：
						String fileType = "";
						if (realFileName.lastIndexOf(".") >= 0) {
							fileType = realFileName.substring(realFileName.lastIndexOf("."));
						}

						String filePath = uploadFileAction(multipartFile);
						if(StringUtils.isNotEmpty(filePath)) {
							UploadResultVO uploadResultVo = new UploadResultVO();
							uploadResultVo.setAttachName(realFileName);
							uploadResultVo.setAttachSize(StringUtils.getFormatDouble(NumberUtils.toDouble(multipartFile.getSize())/1024d,"#0.00"));
							uploadResultVo.setAttachmentSubType("1");/// 1：附件 2：图片 3：视频 4：录音
							uploadResultVo.setAttachPath(filePath);

							if(StringUtils.isNotEmpty(filePath) && !StringUtils.equalsIgnoreCase(filePath,"null")){
								lstUploadResultVO.add(uploadResultVo);
							}
						}
		            }
	            }
	        }
		}
		return lstUploadResultVO;
	}

}
